﻿package mortal.game.view.chat.chatPrivate
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.mvc.core.*;

    public class ChatGuildWindow extends ChatBaseWindow
    {
        public var guildId:SEntityId;
        protected var _btnDice:TimeLoadingButton;
        protected var _btnBattle:TimeLoadingButton;
        protected var _btnRedPack:GLoadingButton;
        protected var _rightTitleText1:GImageBitmap;
        protected var _guildNotice:GTextFiled;
        protected var _rightTitleText2:GImageBitmap;
        protected var _memberList:GTileList;
        private var _isRequestMember:Boolean = false;

        public function ChatGuildWindow()
        {
            title = Language.getString(11508);
            return;
        }// end function

        override protected function initTargetIcon() : void
        {
            _chatIcon = new ChatGuildIcon();
            _chatIcon.targetWindow = this;
            return;
        }// end function

        override public function getChatPlayerEntityId() : SEntityId
        {
            return this.guildId;
        }// end function

        override protected function SendMsg() : void
        {
            if (_inputText.text && _btnSend.enabled)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatSend, {content:getSendContent(), area:ChatArea.Union, color:_color, isHaveMapPoint:_isHasMapPoint}));
                _inputText.text = "";
                _btnSend.startCoolDown();
            }
            return;
        }// end function

        override protected function createFuns() : void
        {
            _btnFace = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetFaceBtn, 0, 0, 24, 24, _areaFuns);
            _btnColorSelector = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetColorBtn, 0, 0, 24, 24, _areaFuns);
            _btnMapPoint = UIFactory.gLoadingButton(ResFileConst.ChatPointBtn, 0, 0, 24, 24, _areaFuns);
            this._btnDice = UIFactory.timeLoadingButton(ResFileConst.ChatDiceBtn, ChatUtil.chatCd, TimeButton.CD, Cache.instance.cd.CHAT, 0, 0, 24, 24, _areaFuns);
            this._btnBattle = UIFactory.timeLoadingButton(ResFileConst.ChatBattleBtn, 30000, TimeButton.CD, Cache.instance.cd.GuildBattle, 0, 0, 24, 24, _areaFuns);
            this._btnRedPack = UIFactory.gLoadingButton(ResFileConst.GiveRedPackBtn, 0, 0, 24, 24, _areaFuns);
            _btnBrush = UIFactory.gLoadingButton(ResFileConst.ChatClearBtn, 0, 0, 24, 24, _areaFuns);
            _btnFace.toolTipData = Language.getString(11502);
            _btnColorSelector.toolTipData = Language.getString(11503);
            _btnMapPoint.toolTipData = Language.getString(11504);
            this._btnDice.toolTipData = Language.getString(11509);
            _btnBrush.toolTipData = Language.getString(11505);
            this._btnBattle.toolTipData = Language.getString(11510);
            this._btnRedPack.toolTipData = Language.getString(11511);
            return;
        }// end function

        override protected function createRightPart() : void
        {
            super.createRightPart();
            UIFactory.bg(4, 122, 170, 26, _rightSprite, ImagesConst.SkillTitle_nameBg);
            this._rightTitleText1 = UIFactory.gImageBitmap(ImagesConst.ChatGuildNotice, 12, 7, _rightSprite);
            this._rightTitleText2 = UIFactory.gImageBitmap(ImagesConst.ChatGuildMember, 12, 125, _rightSprite);
            this._guildNotice = UIFactory.gTextField("", 8, 30, 160, 90, _rightSprite, GlobalStyle.textFormatPutong.setLeading(2));
            this._guildNotice.multiline = true;
            this._guildNotice.wordWrap = true;
            this._memberList = UIFactory.tileList(8, 155, 160, 255, _rightSprite);
            this._memberList.setStyle("cellRenderer", GuildMemberCellRenderer);
            this._memberList.rowHeight = 22;
            this._memberList.columnWidth = 150;
            this._memberList.direction = GBoxDirection.VERTICAL;
            return;
        }// end function

        override protected function addEventListeners() : void
        {
            super.addEventListeners();
            this._btnDice.addEventListener(MouseEvent.CLICK, this.onBtnDiceClick);
            this._btnBattle.addEventListener(MouseEvent.CLICK, this.onBtnBattleClick);
            this._btnRedPack.addEventListener(MouseEvent.CLICK, this.onClickRedPackBtn);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        protected function onBtnDiceClick(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ChatDice));
            return;
        }// end function

        protected function onBtnBattleClick(event:MouseEvent) : void
        {
            if (Cache.instance.role.entityInfo.level < 53)
            {
                MsgManager.showRollTipsMsg(Language.getString(11512));
                return;
            }
            CopyUtil.copyCheck(this.requestBattle);
            return;
        }// end function

        private function requestBattle() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ChatGuildBattle));
            return;
        }// end function

        protected function onClickRedPackBtn(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.RedPackSelfGiveWinOpen, null));
            return;
        }// end function

        override protected function onAddToStage(event:Event) : void
        {
            super.onAddToStage(event);
            if (!this._isRequestMember)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_memberList_get));
                this._isRequestMember = true;
            }
            NetDispatcher.addCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.onGuildInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.GUILD_MEMBERS_UPDATE, this.onGuildInfoUpdate);
            return;
        }// end function

        protected function onRemoveFromStage(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_INFO_UPDATE, this.onGuildInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.GUILD_MEMBERS_UPDATE, this.onGuildInfoUpdate);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.updateGuildMsg();
            return;
        }// end function

        protected function onGuildInfoUpdate(param1 = null) : void
        {
            this.updateGuildMsg();
            return;
        }// end function

        public function updateGuildMsg() : void
        {
            var _loc_1:Array = null;
            if (Cache.instance.guild.selfGuildInfo.selfHasJoinGuild)
            {
                if (Cache.instance.guild.selfGuildInfo.baseInfo.purpose)
                {
                    this._guildNotice.text = Cache.instance.guild.selfGuildInfo.baseInfo.purpose;
                }
                else
                {
                    this._guildNotice.text = Language.getString(11513);
                }
                _loc_1 = Cache.instance.guild.selfGuildInfo.memberList;
                _loc_1.sort(this.onSort);
                this._memberList.dataProvider = new DataProvider(_loc_1);
                this._memberList.drawNow();
            }
            else
            {
                removeThis();
            }
            return;
        }// end function

        protected function onSort(param1:SGuildMember, param2:SGuildMember) : int
        {
            if (param1.miniPlayer.online > param2.miniPlayer.online)
            {
                return -1;
            }
            if (param1.miniPlayer.online < param2.miniPlayer.online)
            {
                return 1;
            }
            if (param1.position > param2.position)
            {
                return -1;
            }
            if (param1.position < param2.position)
            {
                return 1;
            }
            return 0;
        }// end function

    }
}
